package com.ge.modules.sys.controller;

import com.ge.common.utils.DeleteType;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.entity.TbStoreEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 店铺
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-08-07 14:42:15
 */
@RestController
@RequestMapping("/sys/store")
public class SysStoreController {
    @Autowired
    private TbStoreService storeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TbStoreEntity> storeList = storeService.queryList(query);
        int total = storeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(storeList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{storeId}")
    @RequiresPermissions("store:info")
    public R info(@PathVariable("storeId") Integer storeId) {
        TbStoreEntity store = storeService.queryByStoreId(storeId);

        return R.ok().put("store", store);
    }

    /**
     * 添加店铺
     *
     * @param store
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions("store:save")
    public R save(@RequestBody TbStoreEntity store) {
        store.setCreateTime(new Date());
        store.setDeleteType(DeleteType.NOT_DELETE.getKey());
        store.setUserType(3);
        //密码初始化为123456
        store.setPassword(DigestUtils.sha256Hex("123456"));
        if(store.getBackgroundPicture() == null || store.getBackgroundPicture().equals("")){
            store.setBackgroundPicture("[\"\"]");
        }
//        store.setBackgroundPicture("['']");
        storeService.save(store);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:update")
    public R update(@RequestBody TbStoreEntity store) {
        storeService.update(store);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:delete")
    public R delete(@RequestBody Integer[] storeIds) {
        storeService.deleteBatch(storeIds);
        return R.ok();
    }

}
